package com.szhuang.mobilesafe.receiver;

import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.text.TextUtils;

import com.szhuang.mobilesafe.R;
import com.szhuang.mobilesafe.service.GPSService;

/**
 * Created by szhuang on 2015/3/25.
 */
public class SMSReceiver extends BroadcastReceiver {


    private SharedPreferences sp;
    DevicePolicyManager devicePolicyManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] objects = (Object[]) intent.getExtras().get("pdus");
        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        devicePolicyManager = (DevicePolicyManager) context.getSystemService(context.DEVICE_POLICY_SERVICE);

        for (Object obj : objects) {
            SmsMessage sms = SmsMessage.createFromPdu((byte[]) obj);
            String sender = sms.getOriginatingAddress();
            String safePhone = sp.getString("safephone", "");
            String body = sms.getMessageBody();
            if (sender.contains(safePhone)) {
                if ("#*location*#".equals(body)) {
                    System.out.println("位置追踪");
                    abortBroadcast();
                    Intent i = new Intent(context, GPSService.class);
                    context.startService(i);
                    SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
                    String lastlocation = sp.getString("lastlocation", null);
                    if (TextUtils.isEmpty(lastlocation)) {
                        SmsManager.getDefault().sendTextMessage(sender, null, "geting loaction.....", null, null);
                    } else {
                        SmsManager.getDefault().sendTextMessage(sender, null, lastlocation, null, null);
                    }
                } else if ("#*alarm*#".equals(body)) {
                    abortBroadcast();
                    System.out.println("音乐报警");
                    MediaPlayer player = MediaPlayer.create(context, R.raw.alarm);
                    player.setLooping(false);
                    player.setVolume(1.0f, 1.0f);
                    player.start();
                } else if ("#*wipedata*#".equals(body)) {

                    ComponentName componentName = new ComponentName(context, MyAdmin.class);
                    boolean isAdminActive = devicePolicyManager.isAdminActive(componentName);
                    if (isAdminActive) {
                        destroyData();
                    }
                    abortBroadcast();
                    System.out.println("数据销毁");

                } else if ("#*lockscreen*#".equals(body)) {
                    devicePolicyManager = (DevicePolicyManager) context.getSystemService(context.DEVICE_POLICY_SERVICE);
                    ComponentName componentName = new ComponentName(context, MyAdmin.class);
                    boolean isAdminActive = devicePolicyManager.isAdminActive(componentName);
                    if (isAdminActive) {
                        lockScreen();
                    }

                    abortBroadcast();
                    System.out.println("一键锁屏");
                }
            }
        }
    }

    public void lockScreen(){
        devicePolicyManager.lockNow(); // 锁屏
        devicePolicyManager.resetPassword("123", 0); // 设置锁屏密码
    }

    public void destroyData(){
        devicePolicyManager.wipeData(0);  //恢复出厂设置  (建议大家不要在真机上测试) 模拟器不支持该操作
    }


}
